package baiwa.rta.action.user;

import static com.opensymphony.xwork2.Action.SUCCESS;

import java.util.LinkedHashMap;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;


import baiwa.rta.action.BaseAction;
import baiwa.rta.model.DataTableModel;
import baiwa.rta.model.JQueryDataTableParamModel;
import baiwa.rta.model.User;
import baiwa.rta.service.UserService;
import baiwa.rta.utils.DataTablesParamUtility;

@Namespaces(value = { @Namespace("/user") })
@ParentPackage(value = "json-default")
public class USR001JsonAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(USR001JsonAction.class);
	private LinkedHashMap<String, Object> jsonData = new LinkedHashMap<>();
	private JQueryDataTableParamModel<User> tableUserModel = null;
	private User user = null;
	private UserService userService = lookupBean(UserService.class, "UserService");
		

	public String execute() throws Exception {
		logger.info("USR001JsonAction Home.");
		return SUCCESS;
	}

	@Action(value = "getAllUser", results = { @Result(name = SUCCESS, type = "json", params = { "root", "jsonData" }) })
	public String getAllUser() throws Exception {
		logger.info("USR001JsonAction execute lists >>>> user: "+ToStringBuilder.reflectionToString(user));
		
		jsonData.clear();
		tableUserModel = DataTablesParamUtility.getParam(getRequest());
		
		DataTableModel<User> dataTableModel = userService.getUsersDataTable(
				user, 
				tableUserModel.iDisplayStart,
				tableUserModel.iDisplayLength
			);
		
		if (null != dataTableModel) {
			jsonData.putAll(tableUserModel.toJSON(dataTableModel));
		}
		
		
		return SUCCESS;
	}

	@JSON(name = "jsonData")
	public LinkedHashMap<String, Object> getJsonData() {
		return jsonData;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@JSON(name = "tableUserModel")
	public JQueryDataTableParamModel<User> getTableUserModel() {
		return tableUserModel;
	}

	public void setTableUserModel(JQueryDataTableParamModel<User> tableUserModel) {
		this.tableUserModel = tableUserModel;
	}

	

}
